class UserLogicController {
    
    def authenticateService

    def changePassword = {
        log.info "UserLogic.changePassword params.id = ${params.id}"
        def person = User.get(params.id)
        ['person': person , 'id' : params.id]
    }

    def savePassword = {
        def person = User.get(params.id)
        log.info "userlogic.savePassword ${authenticateService.ifNotGranted('ROLE_ADMIN')}" 
        if (authenticateService.ifNotGranted("ROLE_ADMIN") && params.id.toLong() != authenticateService.userDomain()?.id) {
            flash.message = "你无权修改其他人的密码。"
            return render( view: 'changePassword', model: params)
        }
        if (!person) {
            flash.message = "没有找到这个用户。"
            return render( view: 'changePassword', model: params)
        }   
        if (person.passwd != authenticateService.encodePassword(params.oldpassword)) {
            flash.message = "请确认原密码正确。"
            return render( view: 'changePassword', model: params)
        } 
        if (params.password != params.repassword) {
            flash.message = "新密码和确认密码必须相同。"
            return render( view: 'changePassword', model: params)
        }
        person.passwd = authenticateService.encodePassword(params.password)
        person.save()

        redirect action: 'list',controller: 'user'
    }
}
